package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.pojo.VideoHistory;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.pojo.VideoVideo;
import com.cl.short_video_java.service.VideoHistoryService;
import com.cl.short_video_java.mapper.VideoHistoryMapper;
import com.cl.short_video_java.service.VideoUserService;
import com.cl.short_video_java.service.VideoVideoService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author object
 * @description 针对表【video_history】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class VideoHistoryServiceImpl extends ServiceImpl<VideoHistoryMapper, VideoHistory>
		implements VideoHistoryService {
	private VideoUserService userService;
	private VideoVideoService videoService;

	VideoHistoryServiceImpl(VideoUserService userService, VideoVideoService videoService) {
		this.userService = userService;
		this.videoService = videoService;
	}

	@Override
	public Page<VideoHistory> getRecording(Integer pageNum, HttpServletRequest request) {
		VideoUser user = userService.getCurrentUser(request, Const.GRADE_USER);
		Page<VideoHistory> historyPage = this.page(new Page<>(pageNum, 10), new QueryWrapper<VideoHistory>()
				.eq("user_id", user.getId()).orderByDesc("create_time"));
		for (VideoHistory history : historyPage.getRecords()) {
			VideoVideo video = videoService.getOne(new QueryWrapper<VideoVideo>()
					.select("id", "title", "`desc`", "look_count", "first_img_url", "thumb_up", "collection","user_id")
					.eq("id", history.getVideoId())
					.eq("is_delete", 0)
					.eq("status", 1)
			);
			if (video != null) {
				VideoUser videoUser = userService.getOne(new QueryWrapper<VideoUser>()
						.select("id", "username", "nickname")
						.eq("id", video.getUserId()));
				history.setUser(videoUser);
			}
			history.setVideo(video);
		}
		return historyPage;
	}
}




